package com.android.vspice;

import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;

public class VisionSpice extends Activity {
	CameraPreview preview;
	View viewButton;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        disableScreenTurnOff();
        setOrientation();
        setFullscreen();
        
        preview = new CameraPreview(this, 640, 480);
        
        setContentView(preview);
        LayoutInflater inflater = getLayoutInflater();
        viewButton = inflater.inflate(R.layout.preview, null);
        addContentView(viewButton, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        ImageButton btnFocus = (ImageButton)findViewById(R.id.button_focus);
        btnFocus.setOnClickListener(listenFocus);
        ImageButton btnTake = (ImageButton)findViewById(R.id.button_take);
        btnTake.setOnClickListener(listenTake);
    }
    
    /**
	 * Avoid that the screen get's turned off by the system.
	 */
	public void disableScreenTurnOff() {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	/**
	 * Set's the orientation to landscape, as this is needed by AndAR.
	 */
	public void setOrientation() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * Maximize the application.
	 */
	public void setFullscreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void setNoTitle() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	protected void onPause() {
		super.onPause();
		preview.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		preview.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("Analysis");		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getTitle().equals("Analysis")) {
			
		}
		
		return true;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Dialog dialog = null;
		
		return dialog;
	}

	private OnClickListener listenFocus = new OnClickListener() {
		@Override
		public void onClick(View v) {
			preview.setFocus();
		}
	};
	
	private OnClickListener listenTake = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(preview.takePicture()) {
				//viewButton.setVisibility(View.GONE);
			}else {
				//viewButton.setVisibility(View.VISIBLE);
			}
		}
	};
}